package cn.edu.hbwe.book.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import cn.edu.hbwe.book.entity.Order;
import cn.edu.hbwe.book.entity.User;
import cn.edu.hbwe.book.utils.DataSourceUtils;

public class OrderDao {
//生成订单
	public void addProduct(Order order) throws SQLException {
		QueryRunner runner = new QueryRunner();
	
		String sql = "insert into orders(id,money,receiverAddress,receiverName,receiverPhone,paystate,ordertime,user_id) values(?,?,?,?,?,?,?,?)";
		Object[] params = {order.getId(),order.getMoney(),order.getReceiverAddress(),order.getReceiverName(),order.getReceiverPhone(),order.getPaystate(),order.getOrdertime(),order.getUser().getId()};
		int row = runner.update( DataSourceUtils.getConnection(),sql, params);
		if(row==0) {
			throw new RuntimeException();
		}
		
	}
//查找用户信息
	public List<Order> findOrderByUser(final User user) throws SQLException {
		QueryRunner runner = new QueryRunner();

		String sql = "select * from orders where user_id=?";

		return runner.query(DataSourceUtils.getConnection(),sql, new ResultSetHandler<List<Order>>() {
			public List<Order> handle(ResultSet rs) throws SQLException {
				List<Order> orders = new ArrayList<Order>();
				while (rs.next()) {
					Order order = new Order();
					order.setId(rs.getString("id"));
					order.setMoney(rs.getDouble("money"));
					order.setOrdertime(rs.getDate("ordertime"));
					order.setPaystate(rs.getInt("paystate"));
					order.setReceiverAddress(rs.getString("receiverAddress"));
					order.setReceiverName(rs.getString("receiverName"));
					order.setReceiverPhone(rs.getString("receiverPhone"));
					order.setUser(user);
					orders.add(order);
				}
				return orders;
			}
		}, user.getId());
	}
	public Order findOrderById(String id) throws SQLException {
		QueryRunner runner = new QueryRunner();
	
		String sql = "select * from orders,user where orders.user_id=user.id and orders.id=?";

		return runner.query(DataSourceUtils.getConnection(),sql, new ResultSetHandler<Order>() {
			public Order handle(ResultSet rs) throws SQLException {
				Order order = new Order();
				while (rs.next()) {
					order.setId(rs.getString("orders.id"));
					order.setMoney(rs.getDouble("orders.money"));
					order.setOrdertime(rs.getDate("orders.ordertime"));
					order.setPaystate(rs.getInt("orders.paystate"));
					order.setReceiverAddress(rs.getString("orders.receiverAddress"));
					order.setReceiverName(rs.getString("orders.receiverName"));
					order.setReceiverPhone(rs.getString("orders.receiverPhone"));

					User user = new User();
					user.setId(rs.getInt("user.id"));
					user.setEmail(rs.getString("user.email"));
					user.setGender(rs.getString("user.gender"));
					user.setActiveCode(rs.getString("user.activecode"));
					user.setIntroduce(rs.getString("user.introduce"));
					user.setPassword(rs.getString("user.password"));
					user.setRegistTime(rs.getDate("user.registtime"));
					user.setRole(rs.getString("user.role"));
					user.setState(rs.getInt("user.state"));
					user.setTelephone(rs.getString("user.telephone"));
					user.setUsername(rs.getString("user.username"));
					order.setUser(user);
				}
				return order;
			}
		}, id);
	}
//后台查询所有订单
	public List<Order> findAllOrder() throws SQLException {
		QueryRunner runner = new QueryRunner();
	
		String sql = "select orders.*,user.* from orders,user where user.id=orders.user_id order by orders.user_id";

		return runner.query(DataSourceUtils.getConnection(),sql, new ResultSetHandler<List<Order>>() {			
			public List<Order> handle(ResultSet rs) throws SQLException {

				List<Order> orders = new ArrayList<Order>();
  
				while (rs.next()) {
					Order order = new Order();
					order.setId(rs.getString("orders.id"));
					order.setMoney(rs.getDouble("orders.money"));
					order.setOrdertime(rs.getDate("orders.ordertime"));
					order.setPaystate(rs.getInt("orders.paystate"));
					order.setReceiverAddress(rs.getString("orders.receiverAddress"));
					order.setReceiverName(rs.getString("orders.receiverName"));
					order.setReceiverPhone(rs.getString("orders.receiverPhone"));
					orders.add(order);

					User user = new User();
					user.setId(rs.getInt("user.id"));
					user.setEmail(rs.getString("user.email"));
					user.setGender(rs.getString("user.gender"));
					user.setActiveCode(rs.getString("user.activecode"));
					user.setIntroduce(rs.getString("user.introduce"));
					user.setPassword(rs.getString("user.password"));
					user.setRegistTime(rs.getDate("user.registtime"));
					user.setRole(rs.getString("user.role"));
					user.setState(rs.getInt("user.state"));
					user.setTelephone(rs.getString("user.telephone"));
					user.setUsername(rs.getString("user.username"));
					order.setUser(user);
				}
				return orders;
			}
		});
	}
//根据订单号修改订单状态
	public void updateOrderState(String id) throws SQLException {
		QueryRunner runner = new QueryRunner();
	
		String sql = "update orders set paystate=1 where id=?";

		runner.update(DataSourceUtils.getConnection(),sql, id);
		System.out.println(runner.update(DataSourceUtils.getConnection(),sql, id));
	}
//多条件查询
	public List<Order> findOrderByManyCondition(String id, String receiverName)
			throws SQLException {
		QueryRunner runner = new QueryRunner();
	
		List<Object> objs = new ArrayList<Object>();

		String sql = "select orders.*,user.* from orders,user where user.id=orders.user_id ";
		if (id != null && id.trim().length() > 0) {
			sql += " and orders.id=?";
			objs.add(id);
		}
		if (receiverName != null && receiverName.trim().length() > 0) {
			sql += " and receiverName=?";
			objs.add(receiverName);
		}
		sql += " order by orders.user_id";

		return runner.query(DataSourceUtils.getConnection(),sql, new ResultSetHandler<List<Order>>() {
			public List<Order> handle(ResultSet rs) throws SQLException {
				List<Order> orders = new ArrayList<Order>();
				while (rs.next()) {
					Order order = new Order();
					order.setId(rs.getString("orders.id"));
					order.setMoney(rs.getDouble("orders.money"));
					order.setOrdertime(rs.getDate("orders.ordertime"));
					order.setPaystate(rs.getInt("orders.paystate"));
					order.setReceiverAddress(rs
							.getString("orders.receiverAddress"));
					order.setReceiverName(rs.getString("orders.receiverName"));
					order.setReceiverPhone(rs.getString("orders.receiverPhone"));
					orders.add(order);
					User user = new User();
					user.setId(rs.getInt("user.id"));
					user.setEmail(rs.getString("user.email"));
					user.setGender(rs.getString("user.gender"));
					user.setActiveCode(rs.getString("user.activecode"));
					user.setIntroduce(rs.getString("user.introduce"));
					user.setPassword(rs.getString("user.password"));
					user.setRegistTime(rs.getDate("user.registtime"));
					user.setRole(rs.getString("user.role"));
					user.setState(rs.getInt("user.state"));
					user.setTelephone(rs.getString("user.telephone"));
					user.setUsername(rs.getString("user.username"));
					order.setUser(user);

				}

				return orders;
			}
		}, objs.toArray());
	}
	public void delOrderById(String id) throws SQLException {
		
		QueryRunner runner = new QueryRunner();
	
		
		String sql="delete from orders where id=?";		
	
		runner.update(DataSourceUtils.getConnection(),sql,id);		
	}
}
